package com.xps.and.yuntong.Ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jude.utils.JUtils;
import com.xps.and.yuntong.Data.bean1.Resetpwd;
import com.xps.and.yuntong.Data.bean1.ShortBean;
import com.xps.and.yuntong.Data.net.UserNetWorks;
import com.xps.and.yuntong.R;
import com.xps.and.yuntong.Utils.AppCommond;
import com.xps.and.yuntong.Utils.MD5Utils;
import com.xps.and.yuntong.Utils.RegularValidation;
import com.xps.and.yuntong.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;

import rx.Subscriber;

/**
 * Created by administrator on 2017-12-12.
 */

public class XiugaiPasswordTwoActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_fanhui, iv_right;
    private EditText et_phone_number, et_yanzheng_code, et_new_password, et_new_password_again;
    private TextView tv_middle, tv_get_code;
    private LinearLayout ll_xiugai_next;
    private RelativeLayout rl1;

    private String md5Number;
    private String smsModel = "RESET_PWD";
    private int second = 60;

    @Override
    protected int getLayout() {
        return R.layout.activity_xiugai_password_one;
    }

    @Override
    protected void setData() {

    }

    @Override
    protected void init(Bundle inBundle) {
        initView();
    }

    private void initView() {


        iv_fanhui = (ImageView) findViewById(R.id.iv_fanhui);
        iv_right = (ImageView) findViewById(R.id.iv_right);

        et_phone_number = (EditText) findViewById(R.id.et_phone_number);
        et_yanzheng_code = (EditText) findViewById(R.id.et_yanzheng_code);
        et_new_password = (EditText) findViewById(R.id.et_new_password);
        et_new_password_again = (EditText) findViewById(R.id.et_new_password_again);
        tv_get_code = (TextView) findViewById(R.id.tv_get_code);
        tv_middle = (TextView) findViewById(R.id.tv_middle);
        tv_middle.setText(R.string.xiu_password);
        ll_xiugai_next = (LinearLayout) findViewById(R.id.ll_xiugai_next);
        rl1 = (RelativeLayout) findViewById(R.id.rl1);


        iv_fanhui.setOnClickListener(this);
        iv_right.setOnClickListener(this);
        tv_get_code.setOnClickListener(this);
        ll_xiugai_next.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_fanhui:

                finish();
                break;

            case R.id.iv_right:

                break;
            case R.id.tv_get_code:
                if (!isNull(0)) return;
                md5Number = et_phone_number.getText().toString().substring(7, 11) + et_phone_number.getText().toString().substring(0, 3) + smsModel;
                String md5number1 = MD5Utils.MD5Encode(md5Number, "utf-8").toLowerCase();
                Log.e("md5number", md5Number);
                tv_get_code.setEnabled(false);
                handler.postDelayed(runnable, 0);
                getShort(md5number1);
                break;

            case R.id.ll_xiugai_next:
                if (!ableGoNext()) return;
                if (!isNull(1)) return;
                resetpwd();
                break;
        }
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            second--;
            Message msg = handler.obtainMessage();
            if (second > 0) {
                msg.what = 0;
                handler.handleMessage(msg);
                handler.postDelayed(this, 1000);
            } else {
                msg.what = 1;
                handler.handleMessage(msg);
            }
        }
    };
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    tv_get_code.setText("重获" + "(" + second + "s)");
                    break;
                case 1:
                    second = 60;
                    tv_get_code.setText("重新获取");
                    tv_get_code.setEnabled(true);
                    this.removeCallbacks(runnable);
                    break;
            }
        }
    };


    void getShort(String md5number) {
        UserNetWorks.getShort(et_phone_number.getText().toString().trim(), smsModel, md5number, new Subscriber<ShortBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("发送失败");
            }

            @Override
            public void onNext(ShortBean shortBean) {
                if (shortBean.getReturn_code().equals("SUCCESS")) {
                    JUtils.Toast(shortBean.getReturn_msg());
                } else if (shortBean.getReturn_code().equals("FAIL")) {
                    JUtils.Toast(shortBean.getReturn_msg());
                }
            }
        });
    }

    private void resetpwd() {
        UserNetWorks.resetpwd(et_phone_number.getText().toString().trim(), et_yanzheng_code.getText().toString(), et_new_password.getText().toString().trim(), new Subscriber<Resetpwd>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                JUtils.Toast("发送失败");
            }

            @Override
            public void onNext(Resetpwd resetpwd) {
                if (resetpwd.getReturn_code().equals("SUCCESS")) {
//                    JUtils.Toast(resetpwd.getReturn_msg());
                    finish();


                } else if (resetpwd.getReturn_code().equals("FAIL")) {

                    JUtils.Toast(resetpwd.getReturn_msg());
                }
            }
        });


    }

    private boolean isNull(int which) {
        if (which == 0) {
            if (TextUtils.isEmpty(et_phone_number.getText().toString())) {
                Toast.makeText(XiugaiPasswordTwoActivity.this, "请输入手机号！", Toast.LENGTH_SHORT).show();
                return false;
            } else if (!RegularValidation.isMobileNO(et_phone_number.getText().toString())) {
                Toast.makeText(XiugaiPasswordTwoActivity.this, "请输入正确的手机号！", Toast.LENGTH_SHORT).show();
                return false;
            }

        } else {
            if (!et_new_password.getText().toString().trim().equals(et_new_password_again.getText().toString().trim())) {
                Toast.makeText(XiugaiPasswordTwoActivity.this, "两次密码输入不一致，请重新输入！", Toast.LENGTH_SHORT).show();
                return false;
            } else if (et_new_password.getText().toString().trim().length() < 6) {
                Toast.makeText(XiugaiPasswordTwoActivity.this, "密码不能少于6位", Toast.LENGTH_SHORT).show();
                return false;
            }

        }
        return true;
    }

    boolean ableGoNext() {
        if (TextUtils.isEmpty(et_phone_number.getText().toString())) {
            JUtils.Toast("请输入手机号");
            return false;
        } else if (TextUtils.isEmpty(et_yanzheng_code.getText().toString())) {
            JUtils.Toast("请输入验证码");
            return false;
        } else if (TextUtils.isEmpty(et_new_password.getText().toString())) {
            JUtils.Toast("请输入密码");
            return false;
        } else if (TextUtils.isEmpty(et_new_password_again.getText().toString())) {
            JUtils.Toast("请再次输入密码");
            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }
}
